package cn.agiledata.bank.common.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.struts.taglib.TagUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.vnv.infrastructure.util.PagingResult;



public class PageBeanTag extends BodyTagSupport {

	private String name;
	
	private String goImage;
	
	public void setName(Object name) throws JspException {
        this.name = (String) ExpressionEvaluatorManager.evaluate("name", name
                .toString(), Object.class, this, this.pageContext);
    }

    public void setGoImage(Object goImage) throws JspException {
        this.goImage = (String) ExpressionEvaluatorManager.evaluate("goImage",
                goImage.toString(), Object.class, this, this.pageContext);
    }

	private Object getObjFromContext(String property)throws JspException {
		return TagUtils.getInstance().lookup(pageContext, name, property, null);
	}
	
	public int doEndTag() throws JspException {

	StringBuffer sb = new 
	StringBuffer("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
	sb.append("<tr>")
	.append("<td height=\"25\" align=\"center\" nowrap>")
	.append("<input name=\"startPage\" type=\"hidden\" value=\"1\">")
	.append("共 <span class=\"orangefont14b\">")
	.append(getObjFromContext("totalCount"))
	.append("</span> 条 每页显示 <span class=\"orangefont14b\"> ")
	.append(getObjFromContext("length").toString())
	.append("</span> 条 第<span class=\"orangefont14b\">")
	.append(getObjFromContext("curPage").toString())
	.append("</span>页/共<span class=\"orangefont14b\">")
	.append(getObjFromContext("totalPage").toString())
	.append("</span>页 <a href=\"#\" onClick=\"dosubmit('1');\" class=\"menuTextBlue2\">首页</a>")
	.append(" <a href=\"#\" onClick=\"dosubmit('"+getObjFromContext("previous").toString()+"');\" class=\"menuTextBlue2\">")
	.append("上一页</a>")
	.append(" <a href=\"#\" onClick=\"dosubmit('"+getObjFromContext("next").toString()+"');\" class=\"menuTextBlue2\">")
	.append("下一页</a>")
    .append(" <a href=\"#\" onClick=\"dosubmit('"+getObjFromContext("totalPage").toString()+"');\" class=\"menuTextBlue2\">")
	.append("尾页</a> 转到：")
	.append("<input type=\"text\" name=\"goPage\" id=\"goPage\" size=\"1\" onkeyup=\"id_keyup(this)\" value=\"\" class=\"inputPageStyle\"> 页 ");
	if (this.goImage != null && this.goImage.length() > 0) {
	    sb.append("<a href=\"#\" onclick=\"dosubmitGo()\"><img src=\"" + this.goImage + "\" border=\"0\" /></a>");
    } else {
        sb.append("<input type=\"button\" value=\"GO\" onclick=\"dosubmitGo()\" class=\"graybutton_style2\" align=\"absmiddle\">");
    }
	sb.append("</td></tr></table>\n");
	
	sb.append("<script language=\"JavaScript\" type=\"text/javascript\">")
	.append(" function id_keyup(txtinput){txtinput.value=txtinput.value.replace(/(^\\s*)|(\\s*$)/g, \"\");} ")
	.append("function dosubmit(startPage){ ")
	.append("document.forms[\"0\"].startPage.value=startPage;")
	.append("document.forms[\"0\"].submit();")
	.append("}function dosubmitGo(){")
	.append("var a =" + getObjFromContext("totalPage").toString() +";")
	.append("var b=document.getElementById('goPage').value;")
	.append("if(b.indexOf('.')>0){")
	.append("alert('请输入整数！');")
	.append("document.getElementById('goPage').value ='';")
	.append("document.getElementById('goPage').focus();")
	.append("return;")
	.append("}")
	.append("if(isNaN(document.getElementById('goPage').value)==true){")
	.append("alert('请输入数字！');")
	.append("document.getElementById('goPage').value ='';")
	.append("document.getElementById('goPage').focus();")
	.append("return;")
	.append("}")
	.append("if(document.getElementById('goPage').value == ''){")
	.append("alert('请输入页号！');")
	.append("document.getElementById('goPage').focus();")
	.append("return;")
	.append("}")
	.append("if(document.getElementById('goPage').value < 1 ){")
	.append("alert('页号不能小于1！');")
	.append("document.getElementById('goPage').value ='';")
	.append("document.getElementById('goPage').focus();")
	.append("return;")
	.append("}")
	.append("if(document.getElementById('goPage').value > a ){")
	.append("alert('输入页号不能大于总页数！');")
	.append("document.getElementById('goPage').value ='';")
	.append("document.getElementById('goPage').focus();")
	.append("return;")
	.append("}")
	//.append("document.getElementById('dispatch').value='vnvTranList';alert(2);")
	.append("document.forms[\"0\"].startPage.value = document.getElementById('goPage').value; ")
	.append("document.forms[\"0\"].submit();")
	.append("} </script> ");
	
		TagUtils.getInstance()
			.write(this.pageContext, sb.toString());
		return super.doEndTag();
	}
}
